# Copyright (C) 2024 KIDING
# Filename: t_hotkey.py
# Author: lzf
# Date: November 24, 2024

import pyperclip
import keyboard
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
import time
class ClipboardManager:
    def __init__(self): 
        self.clipboard_history = []
        self.max_items = 10
        self.root = tk.Tk()
        self.root.withdraw()
        self.ispaste = False
        self.color_list = ['lightblue', 'lightcyan', 'lightgreen']
   

    def monitor_clipboard(self):
        if self.ispaste: # 如果正在粘贴，则不更新剪贴板历史
            return
        current_clipboard = pyperclip.paste()
        if not self.clipboard_history or self.clipboard_history[0] != current_clipboard:
            if current_clipboard in self.clipboard_history:
                self.clipboard_history.remove(current_clipboard)
            self.clipboard_history.insert(0, current_clipboard)
            if len(self.clipboard_history) > self.max_items:
                self.clipboard_history.pop()
        self.root.after(1000, self.monitor_clipboard)

    def show_clipboard_window(self):
        if self.root.state() == 'normal':
            self.root.withdraw()
            # 设置窗口位置
            self.root.geometry("+{}+{}".format(self.root.winfo_pointerx(), self.root.winfo_pointery()))
            self.root.deiconify()
            return
         

        self.root.deiconify()
        self.root.overrideredirect(True)  # 隐藏标题栏

       
        # 再次设置为最顶层
        self.root.attributes('-topmost', True)
         # 先取消最顶层属性
        self.root.attributes('-topmost', False)
        # 设置窗口位置
        self.root.geometry("+{}+{}".format(self.root.winfo_pointerx(), self.root.winfo_pointery()))

        self.root.lift()  # 提升窗口层级

        for widget in self.root.winfo_children():
            widget.destroy()

        # 添加一个空白行，用于关闭窗口
        close_button = tk.Button(self.root, text="空白", command=self.root.withdraw, relief='flat', bg='lightgray', activebackground='grey')
        close_button.pack(pady=0, fill=tk.X)

        count = 0
        for item in self.clipboard_history:
            count += 1
            button_text = item[:50] + '...' if len(item) > 50 else item
            button = tk.Button(
                self.root,
                text=button_text,
                command=lambda text=item: self.paste_text(text),
                relief='flat',  # 扁平化样式
                bg=self.color_list[count % 3],  # 背景颜色
                activebackground='grey',  # 激活时的背景颜色
                bd=0  # 边框宽度
            )
            button.pack(pady=0, fill=tk.X)

    def paste_text(self, text):
        self.ispaste = True
        last_text = pyperclip.paste()
        pyperclip.copy(text)
        self.root.withdraw()
        time.sleep(0.1)  # 添加短暂延迟
        keyboard.press_and_release('ctrl+v')
        pyperclip.copy(last_text)
        self.ispaste = False

def main():
    manager = ClipboardManager()
    manager.monitor_clipboard()
    keyboard.add_hotkey('ctrl+win', manager.show_clipboard_window)
    tk.mainloop()

if __name__ == "__main__":
    main()
    